//package april.example.keepalive;
//
//import java.io.BufferedReader;
//import java.io.InputStreamReader;
//import java.net.InetSocketAddress;
//import java.util.concurrent.Executors;
//
//import org.jboss.netty.bootstrap.ClientBootstrap;
//import org.jboss.netty.buffer.ChannelBuffer;
//import org.jboss.netty.buffer.ChannelBuffers;
//import org.jboss.netty.channel.Channel;
//import org.jboss.netty.channel.ChannelFactory;
//import org.jboss.netty.channel.ChannelFuture;
//import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
//
//public class ChatClient {
//
//	public static void main(String[] args) throws Exception {
//		String host = "localhost";
//		int port = 6666;
//
//		// Configure the client.
//		ChannelFactory factory = new NioClientSocketChannelFactory(
//				Executors.newCachedThreadPool(),
//				Executors.newCachedThreadPool());
//
//		ClientBootstrap bootstrap = new ClientBootstrap(factory);
//		ClientPipelineFactory cpcf = new ClientPipelineFactory();
//		bootstrap.setPipelineFactory(cpcf);
//
//		// Start the connection attempt.
//		ChannelFuture future = bootstrap.connect(new InetSocketAddress(host,
//				port));
//
//		// Wait until the connection attempt succeeds or fails.
//		Channel channel = future.awaitUninterruptibly().getChannel();
//		if (!future.isSuccess()) {
//			future.getCause().printStackTrace();
//			System.exit(0);
//		}
//
//		// Read commands from the stdin.
//		ChannelFuture lastWriteFuture = null;
//		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//		// for (;;) {
//		// String line = in.readLine();
//		// if (line == null) {
//		// break;
//		// }
//
//		// 创建Builder
//		MessageInfo.Builder builder = MessageInfo.newBuilder();
//
//		builder.addBody(Message.Body.newBuilder().setKey("message")
//				.setValue("你在干什么？" + "/n").build());
//
//		// 创建 赋值结束的 Build 并生成 MessageInfo对象
//		MessageInfo messageInfo = builder.build();
//		// 将messageInfo转换为字节
//		byte[] messageByte = messageInfo.toByteArray();
//
//		// 获得此对象的长度
//		ChannelBuffer channelBuffer = ChannelBuffers.buffer(messageByte.length);
//		// 将 获得到的数组写入 channelBuffer中
//		channelBuffer.writeBytes(messageByte);
//		// 发送到服务器端
//		for (int i = 0; i < 10; i++) {
//			lastWriteFuture = channel.write(channelBuffer);
//		}
//		if (lastWriteFuture != null) {
//			lastWriteFuture.awaitUninterruptibly();
//		}
//		// }
//		// Thread.sleep(50000);
//		// Wait until all messages are flushed before closing the channel.
//
//		Thread.sleep(50000);
//		// Close the connection. Make sure the close operation ends because
//		// all I/O operations are asynchronous in Netty.
//		channel.close().awaitUninterruptibly();
//
//		// We should shut down all thread pools here to exit normally.
//		// However, it is just fine to call System.exit(0) because we are
//		// finished with the business.
//		System.exit(0);
//	}
//}
